using System;
using System.IO;
using System.Reflection;
using TouchSmart.Drawing.Interop;

namespace TouchSmart.Drawing
{
    internal class ImageFactory : IImageFactory
    {
        private readonly string _fullPath;

        private IImagingFactory _imagingFactory;
        private IImagingFactory ImagingFactory
        {
            get
            {
                if(this._imagingFactory == null)
                {
                    this._imagingFactory = Interop.ImagingFactory.GetImaging();
                }
                return _imagingFactory;
            }
        }

        public ImageFactory(string imagesPath)
        {
            var applicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            this._fullPath = Path.Combine(applicationPath, imagesPath);
        }

        public IImage CreateImage(string fileName)
        {
            var imagePath = Path.Combine(this._fullPath, fileName);

            if (File.Exists(imagePath) == false)
                throw new FileNotFoundException(String.Format("An image file could not be found ({0}).", imagePath));

            IImage image;
            ImagingFactory.CreateImageFromFile(imagePath, out image);

            return image;
        }
    }
}